#ifndef _BPMREADER_CPP_
#define _BPMREADER_CPP_

#include "BPMReader.h"

BPMReader::BPMReader() {
	this->start = 0;
	this->end = 0;
	this->dirty = true;
	this->lapse = 0;
	this->bpm = 0;
}

BPMReader::~BPMReader() {}

void BPMReader::tap() {
	clock_t instant = clock();

	if(this->start == 0) {
		this->start = instant;
	}
	else {
		this->start = this->end;
	}

	this->end = instant;
	/// Indicate that the lapse has to be recalculated
	this->dirty = true;
}

float BPMReader::getLapse() {
	if(this->dirty)
	{
		this->lapse = (float)(this->end - this->start) / (float)CLOCKS_PER_SEC;
		this->dirty = false;
	}

	return this->lapse;
}

float BPMReader::getBpm() {
	if(this->getLapse() > 0)
		return 60 / this->getLapse();
	else
		return 0;
}

#endif // _BPMREADER_CPP_

